package com.mars.boss.weixin.web.contrller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.mars.boss.manager.exception.UserBizException;
import com.mars.boss.weixin.entity.WeixinKeyWord;
import com.mars.boss.weixin.exception.WeixinBizException;
import com.mars.boss.weixin.service.WeixinKeyWordService;
import com.mars.web.common.pojo.DataGrid;
import com.mars.web.common.support.BaseController;

@Controller
@RequestMapping("/weixin/keyword")
public class WeixinKeyWordController extends BaseController {
	
	@Autowired
	private WeixinKeyWordService service ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}

	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public Object add(WeixinKeyWord entity) {
		try {
			this.service.insert(entity) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		
		return setResponseResultAddOk();
	}
	
	@RequestMapping(value="/delete/{ids}", method=RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Object update(WeixinKeyWord entity) {
		try {
			this.service.update(entity) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	@ResponseBody
	public WeixinKeyWord get(@PathVariable Long id) {
		return this.service.selectById(id) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public Object list(WeixinKeyWord entity, Integer page, Integer rows) {
		try {
			PageInfo<WeixinKeyWord> selectPage = this.service.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<WeixinKeyWord> dg = new DataGrid<WeixinKeyWord>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			
			return dg ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
}
